#import "LanternSprite.h"
#import "constant.h"


@implementation LanternSprite
@synthesize lightOnInterval, lightOffInterval;

-(id) init
{
    if( (self = [super init]) )
    {
        [self setSpriteType:kSPRITE_TYPE_LANTERN];        
        
        self.numberLabel = [CCLabelBMFont labelWithString:@"" fntFile:@"Dakota_white_48.fnt"];
        [self.numberLabel setVisible:NO];
        [self addChild:numberLabel];
        
        [self setLightOnInterval:1 + arc4random() % 2 * 0.2];
        [self setLightOffInterval:1 + arc4random() % 5 * 0.3];
        
        [self lightOff];
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

-(void) startActionWithWave:(int)wave
{
	switch (wave) {
		case 1:
			break;
		case 2:
			[self runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:1 angle:180]]];
			break;
		case 3:
			[self runAction:[CCRepeatForever actionWithAction:
							 [CCSequence actions:
							  [CCScaleTo actionWithDuration:1 scale:0], 
							  [CCScaleTo actionWithDuration:1 scale:[self scaleY]], 
							  nil]]];
			break;
		default:
			break;
	}
	
}

-(void) lightOn
{
    [self.numberLabel setVisible:YES];
    
    CCDelayTime *action1 = [CCDelayTime actionWithDuration:lightOnInterval];
    CCCallFunc *action2 = [CCCallFunc actionWithTarget:self selector:@selector(lightOff)];
    [self runAction:[CCSequence actions:action1, action2, nil]];
}

-(void) lightOff
{
    [self.numberLabel setVisible:NO];

    CCDelayTime *action1 = [CCDelayTime actionWithDuration:lightOffInterval];
    CCCallFunc *action2 = [CCCallFunc actionWithTarget:self selector:@selector(lightOn)];
    [self runAction:[CCSequence actions:action1, action2, nil]];
}

/*
#pragma mark Override Function From GeneralSprite
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    bool bIsSpriteActive = [super ccTouchBegan:touch withEvent:event];
    
    if(bIsSpriteActive)
    {
        //Only IDLE is able to touch
        if( [self state] != kSPRITE_STATE_IDLE )
            return NO;
        
        [delegate lanternSpriteTouchBegan:self];
        return YES;
    }
    
    return NO;
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super ccTouchMoved:touch withEvent:event];
    [delegate lanternSpriteTouchMoved:self location:CGPointMake(0, 0)];
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super ccTouchEnded:touch withEvent:event];
    [delegate lanternSpriteTouchEnd:self];
}

-(void) startCorrectEffect
{
    [super startCorrectEffect];
    [delegate lanternStartCorrectEffect:self];

    CCMoveTo *action1 = [CCMoveTo actionWithDuration:2 position:ccp(self.position.x, -100)];
    CCCallFunc *action2 = [CCCallFunc actionWithTarget:self selector:@selector(finishCorrectEffect)];
    [self runAction: [CCSequence actions:action1, action2, nil] ];
}

-(void) startIncorrectEffect
{
    [super startIncorrectEffect];
    [delegate lanternStartIncorrectEffect:self];
    
    CCCallFunc *action1 = [CCCallFunc actionWithTarget:self selector:@selector(finishIncorrectEffect)];
    [self runAction: [CCSequence actions:action1, nil] ];
}

-(void) finishCorrectEffect
{
    [super finishCorrectEffect];
    [delegate lanternFinishCorrectEffect:self];
}

-(void) finishIncorrectEffect
{
    [super finishIncorrectEffect];
    [delegate lanternFinishIncorrectEffect:self];
}
*/

@end
